package com.lyq.org.controller;

import com.lyq.annotation.LYQPermission;
import com.lyq.org.domain.Department;
import com.lyq.org.query.DepartmentQuery;
import com.lyq.basic.utils.PageList;
import com.lyq.org.service.IDepartmentService;
import com.lyq.basic.utils.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

/**
 * @Author:liangyunqian
 * @Date: 2022/12/8  14:32
 * @Description:
 */
@LYQPermission(name = "部门管理",descs = "部门的增删改查")
@RestController
@RequestMapping("/department")
@Api(value = "部门的API",description = "部门的增删改查")
public class DepartmentController {

    @Autowired
    private IDepartmentService departmentService;

    //增 改
    @LYQPermission(name = "添加或修改部门")
    @PutMapping
    @ApiOperation("添加或修改部门")
    public R addOrUpdate(@RequestBody Department department){
        try {
            if (Objects.nonNull(department.getId())) {
                departmentService.update(department);
            }else {
                departmentService.insert(department);
            }
            return R.me();
        } catch (Exception e) {
            e.printStackTrace();
            return R.me().setSuccess(false).setMessage("保存失败!"+e.getMessage());
        }
    }


    //单条删除
    @LYQPermission(name = "根据ID删除部门")
    @ApiOperation("根据ID删除部门")
    @DeleteMapping("/{id}")
    public R deleteById(@ApiParam("主键") @PathVariable("id") Long id){
        try {
            departmentService.deleteById(id);
            return R.me();
        } catch (Exception e) {
            e.printStackTrace();
            return R.me().setSuccess(false).setMessage("删除失败!"+e.getMessage());
        }
    }

    //批量删除
    @LYQPermission(name = "根据IDs批量删除部门")
    @ApiOperation("根据IDs批量删除部门")
    @PatchMapping
    public R batchDelete( @RequestBody List<Long> ids){
        try {
            departmentService.batchDelete(ids);
            return R.me();
        } catch (Exception e) {
            e.printStackTrace();
            return R.me().setSuccess(false).setMessage("批量删除失败!"+e.getMessage());
        }
    }

    //根据id查询
    @LYQPermission(name = "根据ID查询部门")
    @ApiOperation("根据ID查询部门")
    @GetMapping("/{id}")
    public R selectById(@ApiParam("主键") @PathVariable("id") Long id){
        try {
            Department department = departmentService.getById(id);
            return R.me().setData(department);
        } catch (Exception e) {
            e.printStackTrace();
            return R.me().setSuccess(false).setMessage("查询失败!"+e.getMessage());
        }
    }

    //查询所有
    @LYQPermission(name = "查询所有部门")
    @GetMapping
    @ApiOperation("查询所有部门")
    public R getAll(){
        try {
            List<Department> departments = departmentService.getAll();
            return R.me().setData(departments);
        } catch (Exception e) {
            e.printStackTrace();
            return R.me().setSuccess(false).setMessage("查询失败!"+e.getMessage());
        }
    }

    //分页查询
    @LYQPermission(name = "分页查询")
    @PostMapping
    @ApiOperation("分页查询")
    public R listPage(@RequestBody DepartmentQuery query){
        try {
            PageList pageList = departmentService.listPage(query);
            return R.me().setData(pageList);
        } catch (Exception e) {
            e.printStackTrace();
            return R.me().setSuccess(false).setMessage("分页查询失败!"+e.getMessage());
        }
    }

    //查询部门树
    @LYQPermission(name = "部门树")
    @GetMapping("/tree")
    @ApiOperation("查询一级部门及其子部门")
    public R tree(){
        try {
            List<Department> departments = departmentService.tree();
            return R.me().setData(departments);
        } catch (Exception e) {
            e.printStackTrace();
            return R.me().setSuccess(false).setMessage("部门树查询失败!"+e.getMessage());
        }
    }

    //查询子部门树
    @LYQPermission(name = "子部门树")
    @GetMapping("/ctree")
    @ApiOperation("查询一级部门及其子部门的子部门")
    public R ctree(){
        try {
            List<Department> departments = departmentService.ctree();
            return R.me().setData(departments);
        } catch (Exception e) {
            e.printStackTrace();
            return R.me().setSuccess(false).setMessage("子部门树查询失败!"+e.getMessage());
        }
    }

}
